package com.sourcethought.sightnavi.model.sight;

import java.util.ArrayList;

import com.sourcethought.sightnavi.model.BizService;
import com.sourcethought.sightnavi.model.device.Device;

public class SightBSCalculateNearestSight extends BizService {
	private ArrayList<Device> devices;
	private ArrayList<Sight> sights;
	private Device nearestDevice;
	private Sight nearestSight;
	
	@Override
	public Object onExecute() throws Exception {
		
		if(devices == null || sights == null){
			return null;
		}
		
		calculateNearestDevice();
		calculateNearestSight();
		
		return nearestSight;
	}

	protected void calculateNearestDevice(){
		
		for(int i = 0; i < devices.size(); i++){
			Device device = devices.get(i);
			if(nearestDevice == null){
				nearestDevice = device;
			} else {
				if(device.getRssi() > nearestDevice.getRssi()){
					nearestDevice = device;
				}
			}
		}
	}
	
	protected void calculateNearestSight(){
		for(int i = 0; i < sights.size(); i++){
			Sight sight = sights.get(i);
			String sightId = sight.getId().toUpperCase();
			String deviceAddress = nearestDevice.getBluetoothDevice().getAddress().toUpperCase();
			
			if(sightId.equals(deviceAddress)){
				nearestSight = sight;
			}
		}
	}
	
	public ArrayList<Device> getDevices() {
		return devices;
	}

	public void setDevices(ArrayList<Device> devices) {
		this.devices = devices;
	}

	public ArrayList<Sight> getSights() {
		return sights;
	}

	public void setSights(ArrayList<Sight> sights) {
		this.sights = sights;
	}
	
	

}
